#
# Tests running mechanical turk queries through our knowledgebase
#
import argparse
import csv
import os.path
import sys
from bbkb.initial_data_load import load_data_on_startup
from bbkb.KBQuery import query_knowledgebase
from bbkb.KnowledgeBase import KnowledgeBase as kb

parser = argparse.ArgumentParser(description='Test Mechanical Turk queries vs. BBKB')
parser.add_argument('-q','--query',action='store_true', default=False, help='Show text queries')
parser.add_argument('-r','--result',action='store_true', default=False, help='Show KB results')

def run_queries(data_file, show_query=False, show_result=False ):
    """Runs mturk queries through knowledgebase.  Counts success as parsing, not true result"""
    print("*"*40)
    print("Processing: %s" % data_file)
    print("*"*40)
    
    reader = csv.reader(open(data_file,'r'))    
    error = 0
    success = 0
    for line in reader:
        query = line[-1]
        if show_query: print(query)
        try:
            results = query_knowledgebase(query)
            if show_result: print results
            success += 1
        except:
            if show_result: print("Error while processing query")
            error += 1
    print("*"*40)
    print("Success: %5d" % success)
    print("Failure: %5d" % error)
    print("Ratio  : %5.2f" % (success/float(success+error)))
    print("*"*40)
    


if __name__ == '__main__':
    print("Starting Mechanical Turk Analysis...")
    args = parser.parse_args()
    print args
    load_data_on_startup(None)
    mturk_data_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'bbkb/data/mturk')
    for f in os.listdir(mturk_data_dir):
        run_queries(os.path.join(mturk_data_dir,f), args.query, args.result)
    print("...Finished")
